package com.sp.scmdb.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.AlarmBean;
import com.sp.scmdb.bean.AlarmTypeBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.entity.Alarm;
import com.sp.scmdb.entity.AlarmType;
import com.sp.scmdb.entity.CallAlarm;
import com.sp.scmdb.entity.Reader_Alarm;
import com.sp.scmdb.service.MonitorService;

@Controller("monitorAlarmAction")
@Scope("prototype")
public class MonitorAlarmAction extends BaseAction<AlarmBean> {

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(MonitorAlarmAction.class);
	private MonitorService monitorService;
	private String result;
	private String idAlarmType;

	public String getIdAlarmType() {
		return idAlarmType;
	}

	public void setIdAlarmType(String idAlarmType) {
		this.idAlarmType = idAlarmType;
	}

	public String monitorShow() {
		try {
			// 报警列表
			List<AlarmType> listAlarmType=monitorService.getAlarmType();
			List<AlarmTypeBean> listAlarmTypeBean = monitorService
					.getCountOfAlarm(listAlarmType);
			request.setAttribute("listAlarmTypeBean", listAlarmTypeBean);

			JSONArray jo = JSONArray.fromObject(listAlarmTypeBean);
			result = jo.toString();
			return SUCCESS;

		} catch (Exception e) {
			log.error("init reader failed! reason:" + e.getMessage());
			return "fail";
		}
	}

	/**
	 * 根据报警类型获取报警详细信息
	 */
	public String findAlarmByType() {
		UserBean user = (UserBean) session.get("user");
		idAlarmType = request.getParameter("idAlarmType");
		if(!user.getRole().getOperation().equals("查询")){
			StringBuffer button = new StringBuffer();
			if(idAlarmType.equals("9") || idAlarmType.equals("2")){
				button.append("<input name=\"del\" type=\"button\"  class=\"bSave\" value=\"确认\" onclick=\"Super.alarmData('/SCMDB/monitorAlarmAction!confirmCallAlarm.action?id=','1')\"/>&nbsp;");
				button.append("<input name=\"del\" type=\"button\" class=\"bSave\"  value=\"全部确认\" onclick=\"Super.alarmData('/SCMDB/monitorAlarmAction!confirmCallAlarm.action?id=all','0')\"/>");
				request.setAttribute("button", button.toString());
			}else{
				button.append("<input name=\"del\" type=\"button\"  class=\"bSave\" value=\"确认\" onclick=\"Super.alarmData('/SCMDB/monitorAlarmAction!confirmAlarm.action?id=','1')\"/>&nbsp;");
				button.append("<input name=\"del\" type=\"button\" class=\"bSave\"  value=\"全部确认\" onclick=\"Super.alarmData('/SCMDB/monitorAlarmAction!confirmAlarm.action?id=all','0')\"/>");
				request.setAttribute("button", button.toString());
			}
		}
		
		if(idAlarmType.equals("8")){
			List<Reader_Alarm> readerList = monitorService.readerList();
			request.setAttribute("readerList", readerList);
			return "readerList";
		}
		if(idAlarmType.equals("9") || idAlarmType.equals("2")){
			List<CallAlarm> callList = monitorService.callAlarmList(idAlarmType);
			request.setAttribute("callAlarmList", callList);
			return "callAlarm";
		}
		// 报警数据
		List<Alarm> listAlarm = monitorService.findAlarmByType(idAlarmType);
		entity.setAllList(listAlarm);
		entity.setList(listAlarm.subList(entity.getCurrentRow(), entity
				.getLastRow()));
		request.setAttribute("idAlarmType", idAlarmType);
		
		return "alarmSucc";
	}

	/**
	 * 确认报警信息
	 */
	public String confirmAlarm() {

		try {
			String id = request.getParameter("id");
			String flg = monitorService.confirmAlarm(idAlarmType, id);
			if(CommonParamter.CHECK_FAIL.equals(flg)){
				messageSession("0","无报警数据确认！");
			}else{
				messageSession(flg, "报警");
			}
			
			if (log.isInfoEnabled()) {
				log.info("delete suncce");
			}
		} catch (Exception e) {
			log.error("confirm Alarm|reason:" + e.getMessage());
			return "fail";
		}
		return "alarmConfSucc";

	}
	/**
	 * 确认报警信息
	 */
	public String confirmCallAlarm() {
		try {
			String id = request.getParameter("id");
			String flg = monitorService.confirmCallAlarm(idAlarmType, id);
			if(CommonParamter.CHECK_FAIL.equals(flg)){
				messageSession("0","无报警数据确认！");
			}else{
				messageSession(flg, "报警");
			}
			if (log.isInfoEnabled()) {
				log.info("delete suncce");
			}
		} catch (Exception e) {
			log.error("confirm Call Alarm|reason:" + e.getMessage());
			return "fail";
		}
		return "alarmConfSucc";

	}
	public void setMonitorService(MonitorService monitorService) {
		this.monitorService = monitorService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
}
